package com.ivan.toolkit.swing;

import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.JTextComponent;

public class TextComponents {
	
	public static TextComponents at(JTextComponent component) {
		return new TextComponents(component);
	}
	
	private JTextComponent component;
	
	private TextComponents(JTextComponent component) {
		this.component = component;
	}
	
	private static void error(String message) {
		throw new RuntimeException(message);
	}
	
	public TextComponents verifyNotEmpty(String message) {
		if(component.getText().isEmpty()) 
			error(message);
		return this;
	}
	
	public TextComponents verifyEqual(JTextComponent comp2, String message) {
		if(!component.getText().equals(comp2.getText())) 
			error(message);
		return this;
	}
	
	
	public TextComponents verifyMinLength(int min, String message) {
		String text = component.getText();
		if(text.length() < min)
			error(message);
		return this;
	}
	
	public TextComponents verifyMaxLength(int max, String message) {
		String text = component.getText();
		if(text.length() > max)
			error(message);
		return this;
	}
	
	public TextComponents verifyPattern(String pattern, String message) {
		String text = component.getText();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(text);
			if(!m.matches())
				error(message);
		} catch(Exception e) {
			error(message);
		}

		return this;
	}
	
	public TextComponents verifyMobile(String message) {
		String text = component.getText();
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(text);
		if(!m.matches())
			error(message);
		return this;
	}
	
	public TextComponents verifyIPV4(String message) {
		String text = component.getText();
		if(!isIPV4(text))
			error(message);
		return this;
	}
	
	public String toText() {
		return component.getText();
	}
	
	public long toLong() {
		String text = component.getText();
		return Long.parseLong(text);
	}
	
	public int toInteger() {
		String text = component.getText();
		return Integer.parseInt(text);
	}
	
	public int toIntegerHex() {
		return toInteger(10);
	}
	
	public int toInteger( int radix) {
		String text = component.getText();
		return Integer.parseInt(text, radix);
	}
	
	public double toDouble() {
		String text = component.getText();
		return Double.parseDouble(text);
	}
	
	public InetAddress toInetAddress() {
		try {
			String text = component.getText();
			return InetAddress.getByName(text);
		} catch(Exception e) {
			return null;
		}
	}
	
	public static boolean isIPV4(String ip) {
        boolean b = false;
        if(ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){  
            String s[] = ip.split("\\.");  
            if(Integer.parseInt(s[0])<255)  
                if(Integer.parseInt(s[1])<255)  
                    if(Integer.parseInt(s[2])<255)  
                        if(Integer.parseInt(s[3])<255)  
                            b = true;  
        }  
        return b;  

	}


}
